package com.jx.gocomtask.webservice.restcontroller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.jx.gocomtask.webservice.DTO.TaskStatus;
import com.jx.gocomtask.webservice.DTO.TodoStatus;
import com.jx.gocomtask.webservice.entity.ExtTaskMessage;
import com.jx.gocomtask.webservice.entity.ExtTaskTodo;
import com.jx.gocomtask.webservice.repository.ExtTaskMessageRepository;
import com.jx.gocomtask.webservice.repository.ExtTaskTodoRepository;
import com.jx.gocomtask.webservice.service.Route;
import com.jx.gocomtask.webservice.service.RouteExt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import phoenics.common.exception.ParamsException;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "与执行计划的API")
@RestController
@RequestMapping("/schedule")
public class Schedule {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Schedule.class);
	@Autowired
	Route route;
	@Autowired
	RouteExt routeExt;
	@Autowired
	ExtTaskMessageRepository extTaskMessageRepository;
	@Autowired
	ExtTaskTodoRepository extTaskTodoRepository;

	@ApiOperation(value = "建立执行计划", notes = "")  
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage createSchedule(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody   ExtTaskTodo extTaskTodo
			){
		return routeExt.saveExtTaskMessagePlan(extTaskTodo,route, false);
	}
	@ApiOperation(value = "复制执行计划", notes = "")  
	@RequestMapping(value = "/copy/{uuid}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage copySchedule(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody  ExtTaskMessage extTaskMessage,
			@PathVariable("uuid") String id
			){
		ExtTaskMessage extTaskMessage_old=extTaskMessageRepository.findByUuid(id);
		if(extTaskMessage_old==null){
			throw new ParamsException();
		}
		return routeExt.copyExtTaskMessagePlan(extTaskMessage_old,extTaskMessage, route);
	}
	@ApiOperation(value = "执行计划详情", notes = "")  
	@RequestMapping(value = "/view/{uuid}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage viewSchedule(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("uuid") String id
			){
		ExtTaskMessage extTaskMessage_old=extTaskMessageRepository.findByUuid(id);
		if(extTaskMessage_old==null){
			throw new ParamsException();
		}
		return extTaskMessage_old;
	}
	@ApiIgnore
	@ApiOperation(value = "编辑执行计划", notes = "")  
	@RequestMapping(value = "/edit/{uuid}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage editSchedule(
			@RequestParam(value="token",  required=true) String wptoken,
			@RequestBody  ExtTaskMessage extTaskMessage,
			@PathVariable("uuid") String id
			){
		ExtTaskMessage extTaskMessage_old=extTaskMessageRepository.findByUuid(id);
		if(extTaskMessage_old==null){
			throw new ParamsException();
		}
		BeanUtils.copyProperties(extTaskMessage, extTaskMessage_old);
		return extTaskMessageRepository.save(extTaskMessage_old);
	}
	@ApiOperation(value = "关闭任务", notes = "")  
	@RequestMapping(value = "/close/{uuid}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage closeSchedule(
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("uuid") String taskid
			){
		return this.changeStatus(taskid, TaskStatus.OVER);
	}
	@ApiOperation(value = "删除任务", notes = "")  
	@RequestMapping(value = "/delete/{uuid}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public ExtTaskMessage deleteSchedule(
			HttpServletRequest request,
			@RequestParam(value="token",  required=true) String wptoken,
			@PathVariable("uuid") String taskid
			){
		return this.changeStatus(taskid, TaskStatus.DELETE);
	}
	private ExtTaskMessage changeStatus(String taskid,TaskStatus taskStatus){
		ExtTaskMessage extTaskMessage_old=extTaskMessageRepository.findByUuid(taskid);
		if(extTaskMessage_old==null){
			throw new ParamsException();
		}
		if(taskStatus==TaskStatus.OVER){
			extTaskMessage_old.setCloseTime(new Date());
		}
		extTaskMessage_old.setStatus(taskStatus);
		return extTaskMessageRepository.save(extTaskMessage_old);
	}
}
